//收货地址数据模块
//数据操作模块
var sqlHelper = require('../conf/sqlHelper');
var async = require('async');

//添加收货地址信息
function addAddressByCustomerId(addressInfo, callback) {
	var sqlstr = 'insert into shop_address(address_city,address_details,address_code,customer_name,customer_code,mobilephone,telphone,isDefault,customer_Id)values(?,?,?,?,?,?,?,?,?)';
	var param = [addressInfo.getaddress_city(), addressInfo.getaddress_details(), addressInfo.getaddress_code(), addressInfo.getcustomer_name(), addressInfo.getcustomer_code(), addressInfo.getmobilephone(), addressInfo.gettelphone(), addressInfo.getisDefault(), addressInfo.getcustomer_Id()];

	//这里涉及到顺序执行,建议用流程控制因为是串联有关系
	async.waterfall([ //顺序流程1
		function(done) {
			//如果添加收货地址时,设置默认值为1,则重置当前用户所有的收货默认为0再添加信息的数据
			if(addressInfo.getisDefault() == 1) {
				resetAddressDefault(addressInfo.getcustomer_Id(), function(result) {
					// body...
					//清空成功
					done(null, result);
				})
			} else {
				//不需要清空直接返回
				done(null, true);
			}
		}, //流程控制2:判断收货地址条数是否大于等于10条
		function(firstresult, done) {
			//第一个参数为流程一返回结果
			if(firstresult) {
				getAddressCountsByCustomerId(addressInfo.getcustomer_Id(), function(results) {
					//判断是否大于等于10条
					if(results) {
						//已经达到10条不可添加
						done(null, false);
					} else {
						//可以添加
						done(null, true);
					}
				});
			}
		}
	], function(error, result) {
		//前面流程依次执行正确
		if(result) {
			sqlHelper.ExecuteUpdate(sqlstr, param, function(counts) {
				if(counts > 0) {
					callback(true);
				} else {
					callback(false);
				}
			});
		}
	});

}

//如果添加收货地址时,设置默认值为1,则重置当前用户所有的收货默认为0再添加信息的数据
function resetAddressDefault(customer_Id, callback) {
	var sqlstr = 'update shop_address set isDefault=0 where customer_Id=?';
	var param = [customer_Id];

	sqlHelper.ExecuteUpdate(sqlstr, param, function(counts) {
		if(counts > 0) {
			callback(true);
		} else {
			callback(false);
		}
	});
}

//判断已添加收货地址是否大于10条,如果大于等于十条则不能添加
function getAddressCountsByCustomerId(customer_Id, callback) {
	var sqlstr = 'select count(*) as num from shop_address where customer_Id=?';
	var param = [customer_Id];

	sqlHelper.ExecuteReader(sqlstr, param, function(result) {
		var count = result[0].num;
		if(count >= 10) {
			callback(true);
		} else {
			callback(false);
		}
	});
}

//收货地址条数
function getAddressCounts(customer_Id, callback) {
	var sqlstr = 'select count(*) as num from shop_address where customer_Id=?';
	var param = [customer_Id];

	sqlHelper.ExecuteReader(sqlstr, param, function(result) {
		var count = result[0].num;
		callback(count);
	});
}

//根据用户ID和收货ID修改收货地址默认值
function updateAddressDefault(customer_Id, address_Id, callback) {
	var sqlstr = 'UPDATE shop_address set isDefault=0 WHERE customer_Id=?';
	var param = [customer_Id];
	
	var sqlstr1 = 'UPDATE shop_address set isDefault=1 WHERE customer_Id=? and address_Id=?';
	var param1 = [customer_Id,address_Id];

	//顺序执行,先设置0再设置1
	async.waterfall([ //顺序流程1
		function(done) {
			//将当前用户的收货地址默认全部归0
			sqlHelper.ExecuteUpdate(sqlstr, param, function(counts) {
				if(counts > 0) {
					done(null,true);
				} else {
					done(null,false);
				}
			});
		}
	], function(error,result) {
		if(result){
			sqlHelper.ExecuteUpdate(sqlstr1, param1, function(counts) {
				if(counts > 0) {
					callback(true);
				} else {
					callback(false);
				}
			});
		}
	});

}

//加载收货地址列表
function getAddressByCustomerId(customer_Id, callback) {
	var sqlstr = 'select * from shop_address where customer_Id=?';
	var param = [customer_Id];

	sqlHelper.ExecuteReader(sqlstr, param, function(results) {
		if(results) {
			callback(results);
		}
	});
}

//根据当前登录用户ID和要删除的地址ID删除指定收货地址
function deleteAddressByCustomerId(customer_Id,address_Id, callback){
	var sqlstr = 'delete from shop_address where customer_Id=? and address_Id=?';
	var param = [customer_Id,address_Id];

	sqlHelper.ExecuteUpdate(sqlstr, param, function(counts) {
		if(counts > 0) {
			callback(true);
		} else {
			callback(false);
		}
	});
}


exports.addAddressByCustomerId = addAddressByCustomerId;
exports.getAddressByCustomerId = getAddressByCustomerId;
exports.getAddressCounts = getAddressCounts;
exports.updateAddressDefault = updateAddressDefault;
exports.deleteAddressByCustomerId=deleteAddressByCustomerId;